package mz.matafe.bolao

/**
 * Domain Class for an User.
 *
 * <p>Represents an user. Eg.: (Mauricio)
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class User implements Comparable{

    //services
    def authenticateService

    //properties
    String username
    String password
    String firstname
    String surname
    String email
    String contactNumber
    boolean emailShow = true
    String confirmPassword = '[secret]'
    Date dateCreated
    Date lastUpdated
    //User createdBy
    //User updatedBy
    boolean active = true
    Date lastLoginDate
    Date loginDate
    SortedSet roles

    static transients = ['confirmPassword']
    static belongsTo = Role
    static hasMany = [roles: Role, cups:Cup, bets:Bet]
    static mappedBy =  [ cups:'createdBy']

    static constraints = {
        username(unique:true, blank:false, size:4..10)
        password(blank:false, validator: {
            if (it == null || it.length() < 4) return ['tooShort']
        })
        firstname(blank:false, size:1..20)
        surname(blank:false, size:1..20)
        email(email:true, nullable:true, maxSize:50)
        contactNumber(nullable:true, maxSize:20)
        dateCreated()
        lastUpdated()
        //createdBy(nullable:true)
        //updatedBy(nullable:true)
        active()
        lastLoginDate(nullable:true)
        loginDate(nullable:true)
    }

    static mapping = {
        table 'WC_USER'
        sort username:'asc'
        //createdBy column: 'CREATED_BY'
        //updatedBy column: 'UPDATED_BY'
    }
/*
    def beforeInsert = {
        log.info "Before insert a User"
        def loggedUser = authenticateService.userDomain()
        this.createdBy = createdBy ?: User.get(loggedUser.id)
        this.updatedBy = updatedBy ?: User.get(loggedUser.id)
    }

    def beforeUpdate = {
        log.info "Updating User ${id}"
        this.updatedBy = authenticateService.userDomain()
    }

    def afterInsert = {
        log.info "User ${id} inserted by ${createdBy}"
    }

    def afterUpdate = {
        log.info "User ${id} updated by ${updatedBy}"
    }
*/
    String toString(){
        "${this.class.name}:id[$id];username[$username]"
    }

    int compareTo(Object o){
        this.username <=> ((User)o).username
    }
}